﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LinqKit;
using PNProject.Core;
using PNProject.Core.Converter;
using PNProject.EF;
using System.Net.Mail;
using System.Text;

namespace PNProject
{
    public static class StaticData
    {
        public static IRepository<Banner> bannerRepo = IoC.Resolve<IRepository<Banner>>();
        public static IRepository<Support> supportRepo = IoC.Resolve<IRepository<Support>>();
        public static IRepository<XUser> xuserRepo = IoC.Resolve<IRepository<XUser>>();
        public static IRepository<Feedback> feedbackRepo = IoC.Resolve<IRepository<Feedback>>();
        public static IRepository<OneContent> oneContentRepo = IoC.Resolve<IRepository<OneContent>>();
        public static IRepository<Link> linkRepo = IoC.Resolve<IRepository<Link>>();
        public static IRepository<News> newsRepo = IoC.Resolve<IRepository<News>>();
        public static IRepository<Category> cateRepo = IoC.Resolve<IRepository<Category>>();
        public static IRepository<Setting> settingRepo = IoC.Resolve<IRepository<Setting>>();
        public static IRepository<TBEmail> emailRepo = IoC.Resolve<IRepository<TBEmail>>();
        public static IRepository<Library> libRepo = IoC.Resolve<IRepository<Library>>();
        public static IRepository<Item> itemRep = IoC.Resolve<IRepository<Item>>();
        public static IRepository<CMenu> cmenuRepo = IoC.Resolve<IRepository<CMenu>>();
        public static IRepository<Product> proRepo = IoC.Resolve<IRepository<Product>>();
        public static IRepository<Comment> commentRepo = IoC.Resolve<IRepository<Comment>>();
        public static IRepository<LocationMap> locationMapsRepo = IoC.Resolve<IRepository<LocationMap>>();
        public static IRepository<LocationIp> locationIpsRepo = IoC.Resolve<IRepository<LocationIp>>();
        public static IRepository<UserTracking> userTrackingRepo = IoC.Resolve<IRepository<UserTracking>>();
        public static IRepository<ProductIP> productIpRepo = IoC.Resolve<IRepository<ProductIP>>();
        public static string SendEmail(string email, string htlmcontent, string title)
        {
            try
            {
                SmtpClient client = new SmtpClient();
                client.Port = 25;
                client.Host = "localhost";
                client.EnableSsl = false;
                client.Timeout = 10000;
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.UseDefaultCredentials = true;
                client.Credentials = new System.Net.NetworkCredential(Information.AdminEmail, Information.Pass);

                MailMessage mm = new MailMessage(Information.AdminEmail, email, title, htlmcontent);
                mm.IsBodyHtml = true;
                mm.BodyEncoding = UTF8Encoding.UTF8;
                mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

                //client.Send(mm);
                string complete = "ooo";
                client.SendAsync(mm, complete);
                return "ok";
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }
        public static IEnumerable<ItemModel>GetAllProductActive()
        {
            var products = itemRep.GetSome(a => a.IsActive == true && a.ItemType == (int) EnumType.ItemType.Product);
            var productModels = new List<ItemModel>();
                products.ForEach(a =>
                                 {
                                     var item = ItemConverter.ItemTo(a);
                                     productModels.Add(item);
                                 });
            return productModels;
        }
        public static IEnumerable<Product> GetAllProductByCatalog(int cataId)
        {
            var products = proRepo.GetSome(a => a.CategoryID==cataId);
            return products;
        }
        public static IEnumerable<Product> GetAllProduct()
        {
            var products = proRepo.GetAll();
            return products;
        }
        public static Product GetProductbyProductId(Guid id)
        {
            var product = proRepo.GetOne(a=>a.ProductID==id);
            return product;
        }
        public static List<ProductIP> GetProductByIP(string IP, Guid proId)
        {
            var productIps = productIpRepo.GetSome(a => a.ProductId == proId && a.IP==IP);
            if (productIps==null)
                return new List<ProductIP>();
            return productIps.ToList();
        }
    }
}